class Food < ApplicationRecord
    has_attached_file :attachment,
                    styles: { image: "750x750>",icon:'114x114>' },
                    default_url: '/img/no_product_200.jpg',
                    path: ":rails_root/public/uploads/foods/:id/:style/:basename.:extension",
                    url: "/uploads/foods/:id/:style/:basename.:extension"
    belongs_to :food_type,required: false
    has_one :drink,dependent: :destroy
    has_one :discount_food,dependent: :destroy
    has_many :food_group_items,dependent: :destroy
    has_many :food_groups,through: :food_group_items    
    has_many :order_items,dependent: :destroy
    validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/
    validates :name,presence: true
    validates :sell_price,presence: true,numericality: {greater_than: 0}
    validates :show_price,numericality:true,allow_nil: true
    validates_attachment_presence :attachment



    def is_discount_food
        if self.discount_food.present?
            true
        else
            false    
        end    
    end   

    def is_published?
        if is_published
            "已上架"
        else
            "已下架"    
        end    
    end    
    
end
